package com.sxygsj.android.personnelscreen.services.webserver.controller;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.sxygsj.android.personnelscreen.util.HostFileUtils;
import com.tencent.mmkv.MMKV;
import com.yanzhenjie.andserver.annotation.GetMapping;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.ResponseBody;
import com.yanzhenjie.andserver.annotation.RestController;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * @Author LD
 * @Time 2019/11/4 11:05
 * @Describe APK上传更新的网页
 * @Modify
 */
@RestController
public class AdvertisementController {

    /**
     * 广告
     */
    @ResponseBody
    @PostMapping("/AdvertisementUpdate")
    public String dataConfigFun(final Context context, @RequestParam("advertisementFile") MultipartFile file) throws IOException {
        final byte[] fileBytes = file.getBytes();
        final String fileName = file.getFilename();

        Observable.create(new ObservableOnSubscribe<String>() {

            @Override
            public void subscribe(ObservableEmitter<String> emitter) throws Exception {
                //APK写入路径
                String gasIPPath = Environment.getExternalStorageDirectory() + "/com.sxygsj.android.personnelscreen.advertisement/";

                HostFileUtils.writeBytesToFile(fileBytes, gasIPPath, fileName);

                String filePath = Environment.getExternalStorageDirectory() + "/com.sxygsj.android.personnelscreen.advertisement/" + fileName;

                emitter.onNext(filePath);
                emitter.onComplete();
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .subscribe(new Consumer<String>() {
                    @SuppressLint("CheckResult")
                    @Override
                    public void accept(String filePath) throws Exception {
                        MMKV mmkv=MMKV.mmkvWithID("advertisementFile");
                        mmkv.encode("file",filePath);
                         DateFormat dateFormat = DateFormat.getDateTimeInstance();
                        String time = dateFormat.format(new Date());
                        String str = "上传时间：" + time + '\n' + "上传文件名：" + fileName ;
                        mmkv.encode("info",str);
                    }
                });
        return fileName + "上传成功";
    }


    /**
     * 是否安装成功
     *
     * @param context
     * @return
     */
    @ResponseBody
    @GetMapping("/updateInfo")
    public String updateInfo(Context context) {
        //更新信息
        MMKV mmkv=MMKV.mmkvWithID("advertisementFile");
        String file = mmkv.decodeString("file", "");
        if (TextUtils.isEmpty(file)){
            return "暂无上传信息";
        }else {
          return   mmkv.decodeString("info");
        }

    }

}
